$(function () {
    var currentUrl = window.location.href;
    var pop_up_tag = null;
    var logistics_info = null;

    function set_cargo_sumbit() {
        // 物料更新
        var cargo_type = $('input[name="cargo_type"]:checked').val();
        var store_type = $('input[name="store_type"]:checked').val();
        var tp = $(this).attr("tp");
        url = currentUrl
        const formData = {
            "cargo_type": cargo_type,
            "type": tp,
            "store_type": store_type
        };
        djangoAjax({
            url: url,
            data: formData,
            method: "POST",
            loading_call_back: function () {
                $(".sales_status_content").text("执行中，请稍后......")
            },
            success_call_back: function (content) {
                $(".sales_status_content").text("设置成功")
            },
            error_call_back: function (content) {
                $(".sales_status_content").text("设置失败")
            }
        })
    }

    function create_transport_order_sumbit() {
        // 创建装车单
        var plate_number = $(".modal-overlay .modal-content .create_transport_order .modal-body .logistics_plate_number #cars").val();
        var goods_list = [];
        $(".modal-overlay .modal-content .create_transport_order .modal-body .logistics_info_table .logistics_info_table_row:gt(0)").each(function (index, item) {
            console.log("循环第" + index + "行");
            var goods_no = $(this).children(".logistics_info_table_col:nth-child(1)").text();
            var goods_name = $(this).children(".logistics_info_table_col:nth-child(2)").text();
            var goods_count = $(this).children(".logistics_info_table_col:nth-child(5)").children("input").val();
            console.log("获取数据：" + goods_no + ":" + goods_name + ":" + goods_count);
            goods_list.push({
                "goods_no": goods_no,
                "goods_name": goods_name,
                "goods_count": goods_count
            })
        })
        console.log("结束循环");
        console.log("获取列表：" + goods_list);
        var tp = "create_transport_order";
        url = currentUrl
        const formData = {
            "plate_number": plate_number,
            "type": tp,
            "goods_list": JSON.stringify(goods_list)
        };
        djangoAjax({
            url: url,
            data: formData,
            method: "POST",
            loading_call_back: function () {
                $(".logistics_status_content").text("执行中，请稍后......")
            },
            success_call_back: function (content) {
                $(".logistics_status_content").text(content.status)
            },
            error_call_back: function (content) {
                $(".logistics_status_content").text("创建装车单异常")
            }
        })
    }

    $(".set_cargo_masking").click(function () {
        // 设置当前弹窗开启的标识
        pop_up_tag = $(this).attr("tp");
    });

    $(".get_logistics_order_masking").click(function () {
        // 设置当前弹窗开启的标识
        pop_up_tag = $(this).attr("tp");
    });

    $(".btn-set-cargo-sure").on("click", function () {
        // 弹窗提交逻辑
        if (pop_up_tag == "set_cargo") {
            // 设置物料信息
            set_cargo_sumbit();
        } else if (pop_up_tag == "get_logistics_order") {
            // 创建装车单
            create_transport_order_sumbit();
        }

    })
})